package com.example;

import com.example.service.MessageService;
import mapper.CityMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main(String[] args) {
        // 1. 创建Spring容器（基于注解配置）
        ApplicationContext context =
                new AnnotationConfigApplicationContext(AppConfig.class);

        // 2. 从容器中获取Bean
        CityMapper cityMapper = context.getBean(CityMapper.class);

        // 3. 使用Bean
        String info = cityMapper.info();
        System.out.println(">>>>>cityMapper.info: " + info);

        // 4. 查看容器中的所有Bean定义
        System.out.println("\nBeans in container:");
        String[] beanNames = context.getBeanDefinitionNames();
        for (String beanName : beanNames) {
            System.out.println("- " + beanName);
        }

        MessageService messageService = context.getBean(MessageService.class);
        messageService.addCityInfo("hangzhou", "571");

        System.out.println(messageService.getMessage());

        // 5. 关闭容器（对于AnnotationConfigApplicationContext需要手动关闭）
        ((AnnotationConfigApplicationContext) context).close();
    }
}
